<?php

namespace Application\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Application\Model\AbonnementTable;
use Application\Model\Abonnement;
use Zend\Form\Element\Date;
use Zend\Db\Adapter\Adapter as DbAdapter;

class AbonnementController extends AbstractActionController
{

	protected $abonnementTable;
	public function AbonnementAction(){

	
	$request = $this->getRequest ();
	$datas = $request->getPost();
	
	$inscription = $datas['Inscription'];
	$desinscription = $datas['Désinscription'];
	$today = date("Y-m-d");
	
	if($inscription != null && $inscription !="" &&$inscription != " "){
		
		try {
			$this->getAbonnementTable()->addAbonnement($inscription, $today,1);
		} catch (\PDOException $e) {
			 
			throw new Exception\InvalidQueryException(
					'Statement could not be executed ',
					null,
					$e
			);
		}
	
	}else if ($desinscription != null && $desinscription !="" &&$desinscription != " "){
		
		$newdata = array('$set' => array("email" => $desinscription));
		//$c->update(array("firstname" => "Bob"), $newdata);
		
		//array("username" => "joe312", "createdAt" => new MongoDate()),
		$this->getAbonnementTable()->update(array("is_active" => 0),array("email" => $desinscription));
	}
	
	}

	public function getAbonnementTable() {
		if (! $this->abonnementTable) {
			$sm = $this->getServiceLocator ();
			$config = $sm->get ( 'config' );
			$config = $config ['db'];
			$dbAdapter = new DbAdapter ( $config );
			// return $dbAdapter;
			$table = new AbonnementTable( $dbAdapter );
			// $this->userTable = $sm->get($table);
			$this->abonnementTable = $table;
		}
		return $this->abonnementTable;
	}

}
